;$(function() {
	var serverIdArr=[];
    var id = $("#id").val();
    if(id==null||id==""){
    	getCityList("421000","421000",null,null);
    	upload(serverIdArr);
    }else{
    	getInfo(serverIdArr);
    }
    submitForm();
})
var tale = new $.tale();//弹框

function upload(serverIdArr){
    $("#dropzone").dropzone({
        paramName: "file",
        url:  ctxPath+"/file/upload",
        addRemoveLinks: true,
        dictRemoveLinks: "x",
//        uploadMultiple: true,
        maxFilesize: 10,
        init: function () {
            this.on("success", function (file, msg) {
               	file.serverId=msg.message;
            	serverIdArr.push(msg.message);
            	$("#image_files").val(serverIdArr);
            });
            this.on('error', function (files, response) {
                //文件上传失败后的操作
            	console.log(files, response);
            });
            //删除图片的事件，当上传的图片为空时，使上传按钮不可用状态
            this.on("removedfile", function (file) {
            	serverIdArr.splice($.inArray(file.serverId,serverIdArr),1);
            	$("#image_files").val(serverIdArr);
            });
        }
    });
}


function getInfo(serverIdArr){
	   $.ajax({
        url: ctxPath+"/agency/org/getInfo",
        type: 'GET',
        success: function(d){
        	var success = d.success;
        	 if(success==true){
        	 var data=  d.data;
        	 umbinding.bind(data);
        	 $("#map").val(data.lat+","+data.lon);
        	 getCityList(data.cityCode,data.cityCode,data.countyCode,data.streetCode);
        	 ue.ready(function(){//监听编辑器实例化完成的事件
        			ue.setContent(data.content);
        	});
        	 console.log(serverIdArr);
        	 serverIdArr.push(data.files);
        	 $("#image_files").val(serverIdArr);
             var myDropzone = new Dropzone("#dropzone", {
                 paramName: "file",
                 url: ctxPath+"/file/upload",
                 addRemoveLinks: true,
                 dictRemoveLinks: "x",
//     	        uploadMultiple: true,
                 maxFiles: 3,
                 maxFilesize: 10,
                 init: function () {
                     this.on("success", function (file, msg) {
                     	 file.serverId = msg.message;
                          serverIdArr.push(msg.message);
                          $("#image_files").val(serverIdArr);
                     });
                     this.on('error', function (files, response) {
                     });
                     //删除图片的事件，当上传的图片为空时，使上传按钮不可用状态
                     this.on("removedfile", function (file) {
                     	 serverIdArr.splice($.inArray(file.serverId, serverIdArr), 1);
                          $("#image_files").val(serverIdArr);
                     });
                 }
             });
             if (data.files != null) {
                 for (var i = 0; i < data.files.length; i++) {
//                     var size = data.files[i].size * 1024;
                     var mockFile = {
//                         name: data.files[i].srcname,
                         accepted: true,
//                         size: size, //图片尺寸
                         height: "70%",
                         width: "70%",
                         type: '.jpg,.png,.gif'//图片文件类型
                     };
                     myDropzone.emit("addedfile", mockFile);
                     myDropzone.emit("thumbnail", mockFile, ctxPath+"/file/showImage/" + data.files[i]);
                     myDropzone.emit("complete", mockFile);
                 }
             }
        	 }
        }
    });
}


function submitForm(){
	$("#global-form").validator({
		rules: {},
	    fields: {
	    	name:{
				rule : "required;length(~255)",
				msg : {
					required : "必填!"
				}
			},
			detailAddress:{
				rule : "required;length(~500)",
				msg : {
					required : "必填!"
				}
			},
	    },
//	    fields: {},
	    invalid: function(form, errors){
	    	console.log(errors);
	    	$(".btn-submit").removeAttr("style");
	    },
	    //验证成功
	    valid: function(form) {
	    	saveInfo();
	    },
	});
}


function saveInfo(){
	var json =$("#global-form").serializeFormJSON();
	var id = $("#id").val();
    var url = ctxPath+"/agency/org/saveInfo";
    if (id != '' && id != null) {
        url = ctxPath+"/agency/org/updateInfo";
     }
	   $.ajax({
           url: url,
           type: 'POST',
           data: json,
           success: function(d){
            var success = d.success;
            if (success) {
                tale.alertOk({
                    text: '保存成功',
                    then: function () {
                        setTimeout(function () {
                            window.location.href = ctxPath+'/agency/org/index';
                        }, 500);
                    }
                });
            } else {
                tale.alertError(result.message || '保存失败');
            }
           }
       });
}


 function createMap(){
	 $("#mapModal").modal("show");
 	setTimeout(function() {		//添加延时加载。解决问题
 		var map = new BMap.Map("map_main");
 		var myCity = new BMap.LocalCity();
 		myCity.get(function(res){
 			map.centerAndZoom(res.center,res.level); 
 			var old_map = $('#map').val();		//如果已设置过
 			if(old_map.length > '5'){			//打开的时候显示已设置的
 				$("#map_txt").val(old_map);		
 				var oldMap = old_map.split(",");
 				var point = new BMap.Point(oldMap[0],oldMap[1]);    
 				var marker = new BMap.Marker(point);        // 创建标注    
 				map.clearOverlays();
 				map.addOverlay(marker);
 			}
 			map.addEventListener("click", function(e){
 				var lng_lat = e.point.lng+','+e.point.lat;
 				$("#map_txt").val(lng_lat);					//加入到设置框
 				var point = new BMap.Point(e.point.lng,e.point.lat);    
 				var marker = new BMap.Marker(point);        // 创建标注    
 				map.clearOverlays();
 				map.addOverlay(marker);
 			});
 		});
 	},300);
 }

//设置经纬度
function setMapValue(){
if($("#map_txt").val()==""){ alert('你还没选择相应的坐标点^_^哦'); return false; }
$("#map").val($("#map_txt").val());
$('#mapModal').modal('hide')
}